/**
 * @author:cavion(曹仁道)
 * @email:caorendao187@163.com
 * 2018年3月18日
 */
package org.rk.core.nosql.mongodb.baseBean;

import java.util.List;

import org.rk.core.common.util.RkObjectUtil;
import org.springframework.data.mongodb.core.query.Criteria;

/**
 * @author cavion
 * @描述
 * 2018年3月18日 下午10:08:28
 */
public class NoSqlCondition {
	public static final String condi_type_and = "AND";
    public static final String condi_type_or = "OR";
    public static final String condi_type_order = "ORDER";
    
    public static final String condi_mode_like = "LIKE";
    public static final String condi_mode_eq = "eq";
    public static final String condi_mode_lt = "lt";
    public static final String condi_mode_gt = "gt";
    public static final String condi_mode_lt_eq = "eq-lt";
    public static final String condi_mode_gt_eq = "eq-gt";
    
    public static Criteria genCriteria(List<NoSqlParamMap> paramMapList,boolean isChild) {
    	Criteria criteria = null;
    	for (NoSqlParamMap paramMap : paramMapList) {
			if(RkObjectUtil.isEmpty(criteria) && !isChild) {
				criteria = Criteria.where(paramMap.getParamName());
			}
			//and
			if(NoSqlCondition.condi_type_and.equals(paramMap.getCondiType())) {
				criteria.and(paramMap.getParamName());
				if(NoSqlCondition.condi_mode_eq.equals(paramMap.getCondiMode())) {
					criteria.is(paramMap.getParamValue());
				}else if(NoSqlCondition.condi_mode_lt.equals(paramMap.getCondiMode())) {
					criteria.lt(paramMap.getParamValue());
				}else if(NoSqlCondition.condi_mode_gt.equals(paramMap.getCondiMode())) {
					criteria.gt(paramMap.getParamValue());
				}else if(NoSqlCondition.condi_mode_lt_eq.equals(paramMap.getCondiMode())) {
					criteria.lte(paramMap.getParamValue());
				}else if(NoSqlCondition.condi_mode_gt_eq.equals(paramMap.getCondiMode())) {
					criteria.gte(paramMap.getParamValue());
				}
			}else if(NoSqlCondition.condi_type_or.equals(paramMap.getCondiType())) {
				criteria.orOperator(genCriteria(paramMap.getChildParamMap(),true));
			}
		}
    	return criteria;
    }
}
